package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.databinding.DataBindingUtil;

import com.sgb.member.R;
import com.sgb.member.databinding.LayoutMultiEditTextBinding;
import com.swgk.core.util.StringUtil;

public class MultiEditText extends LinearLayout {

    private final LayoutMultiEditTextBinding mMultiBinding;

    private boolean required;

    public MultiEditText(Context context) {
        this(context, null);
    }

    public MultiEditText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mMultiBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.layout_multi_edit_text, null, false);
        if (attrs != null) {
            @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MultiEditText);
            required = typedArray.getBoolean(R.styleable.MultiEditText_multi_required, false);
            String describe = typedArray.getString(R.styleable.MultiEditText_multi_tv_describe);
            SpannableString sStr = new SpannableString(describe);
            if (required) {
                int desIndex = describe.length();
                sStr = new SpannableString(describe + " *");
                sStr.setSpan(new ForegroundColorSpan(Color.RED), desIndex, sStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            mMultiBinding.tvDescribe.setText(sStr);
            //设置最大字数限制
            int integer = typedArray.getInteger(R.styleable.MultiEditText_multi_max_count, 300);
            mMultiBinding.etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(integer)});
            mMultiBinding.overPlusInputTv.setText(String.valueOf(integer));
            //设置hint内容
            mMultiBinding.etContent.setHint(typedArray.getString(R.styleable.MultiEditText_multi_et_hint));
            mMultiBinding.etContent.setText(typedArray.getString(R.styleable.MultiEditText_multi_et_content));
            typedArray.recycle();
        } // 烟台
        addView(mMultiBinding.getRoot());
        mMultiBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mMultiBinding.alreadyInputTv.setText(String.valueOf(editable.length()));
            }
        });
    }

    public String getContent() {
        Editable text = mMultiBinding.etContent.getText();
        if (text == null) {
            return "";
        }
        return text.toString();
    }

    public void setContent(String text) {
        mMultiBinding.etContent.setText(StringUtil.getNotEmptyStr(text));
    }

    public void setContentHint(String text) {
        mMultiBinding.etContent.setHint(text);
    }

    public void setTitle(String text) {
        if (!StringUtil.isEmpty(text)) {
            SpannableString sStr = new SpannableString(text);
            if (required) {
                int desIndex = text.length();
                sStr = new SpannableString(text + " *");
                sStr.setSpan(new ForegroundColorSpan(Color.RED), desIndex, sStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            mMultiBinding.tvDescribe.setText(sStr);
        } else {
            mMultiBinding.tvDescribe.setText("");
        }
    }

    public AppCompatEditText getEditText() {
        return mMultiBinding.etContent;
    }
}
